package com.sgl.blog.controller.blog;

import com.sgl.blog.pojo.*;
import com.sgl.blog.service.LikesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @标题： LikesController
 * @内容：
 * @作者： 宋志豪
 */
@RestController
@RequestMapping("blog")
public class LikesController {
    @Resource
    private LikesService likesService;

    @GetMapping("likes/{id}")
    public Restful<Boolean> isLikes(@PathVariable(name = "id") Integer articleId, HttpSession session){
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        if (account != null){
            BlogLikes blogLikes = new BlogLikes();
            blogLikes.setArticleId(articleId);
            blogLikes.setAccountId(account.getAccountId());
            return RestfulBuilder.success(likesService.findOne(blogLikes), ResultCode.SUCCESS);
        }
        return RestfulBuilder.faile(ResultCode.USER_NOT_LOGGED_IN);
    }

    @PostMapping("likes")
    public Restful<Boolean> like(Integer articleId,HttpSession session){
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        if (account != null){
            BlogLikes blogLikes = new BlogLikes();
            blogLikes.setArticleId(articleId);
            blogLikes.setAccountId(account.getAccountId());
            return RestfulBuilder.success(likesService.onLike(blogLikes), ResultCode.SUCCESS);
        }
        return RestfulBuilder.faile(ResultCode.USER_NOT_LOGGED_IN);
    }

}
